package com.chat.common.common.event.Listener;

import com.chat.common.common.event.UserRegisterEvent;
import com.chat.common.user.dao.UserBackpackDao;
import com.chat.common.user.dao.UserDao;
import com.chat.common.user.domain.entity.User;
import com.chat.common.user.domain.enums.IdempotentEnum;
import com.chat.common.user.domain.enums.ItemEnum;
import com.chat.common.user.service.IUserBackpackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class UserRegisterListener {
    @Resource
    private UserDao userDao;

    @Resource
    private IUserBackpackService userBackpackService;

    @Async
    @EventListener(UserRegisterEvent.class)
    public void sendModifyNameEvent(UserRegisterEvent event) {
        User user = event.getUser();
        //发送改名卡
        userBackpackService.acquireItem(user.getId(), ItemEnum.MODIFY_NAME_CARD.getId(), IdempotentEnum.UID,user.getId().toString());
    }

    @EventListener(UserRegisterEvent.class)
    public void sendBadgeEvent(UserRegisterEvent event) {
        User user = event.getUser();
        //发送前十注册勋章
        int count = userDao.count();
        if(count<=10){
            userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID,user.getId().toString());
        }

    }

}
